package com.gongpingjialoan.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.SearchDealerBean;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2016/9/12.
 */
public class SearchDealerAdapter extends BaseAdapter {

    private List<SearchDealerBean.DataEntity> mDataEntities;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    private DecimalFormat df = new DecimalFormat("0.0");

    public SearchDealerAdapter(List<SearchDealerBean.DataEntity> dataEntities, Context context) {
        mDataEntities = dataEntities;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setData(List<SearchDealerBean.DataEntity> dataEntities) {
        this.mDataEntities = dataEntities;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mDataEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.dealers_layout_item, null);
            holder = new ViewHolder();
            holder.nameTextView = (TextView) convertView.findViewById(R.id.name);
            holder.numTextView = (TextView) convertView.findViewById(R.id.num);
            holder.timeTextView = (TextView) convertView.findViewById(R.id.time);
            holder.addressTextView = (TextView) convertView.findViewById(R.id.address);
            holder.distanceTextView = (TextView) convertView.findViewById(R.id.distance);
            holder.renzhengImageView = (ImageView) convertView.findViewById(R.id.rz);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SearchDealerBean.DataEntity dataEntity = mDataEntities.get(position);
        holder.nameTextView.setText(dataEntity.getCompany_name());
        holder.numTextView.setText(dataEntity.getOn_sale_count() + "");
        if (!TextUtils.isEmpty(dataEntity.getUpdate_time()) && dataEntity.getUpdate_time().contains(" ")) {
            holder.timeTextView.setText("车源最后更新时间 " + dataEntity.getUpdate_time().split(" ")[0]);
        }
        if(!TextUtils.isEmpty(dataEntity.getDistance())) {
            double distance = Double.parseDouble(dataEntity.getDistance()) / 1000;
            holder.distanceTextView.setText(df.format(distance) + "km");
            holder.distanceTextView.setVisibility(View.VISIBLE);
        }else{
            holder.distanceTextView.setVisibility(View.GONE);
        }
        if (dataEntity.isIs_certify()) {
            holder.renzhengImageView.setImageResource(R.drawable.dealer_renzheng);
        } else {
            holder.renzhengImageView.setImageResource(R.drawable.delear_weirenzheng);
        }

        holder.addressTextView.setText(dataEntity.getAddress());


        //holder.addressTextView.setText("地址: " + dataEntity.get);
        return convertView;
    }

    public static class ViewHolder {
        public TextView nameTextView;
        public TextView numTextView;
        public TextView timeTextView;
        public TextView addressTextView;
        public TextView distanceTextView;
        public ImageView renzhengImageView;

    }
}
